package cn.qiku.rf.utils
{
	import cn.qiku.rf.debug.RFTraceSummary;
	import flash.utils.describeType;
	import flash.utils.getDefinitionByName;
	
	public final class VOUtils
	{
		public static function xmltovo(v:Class,x:XML):Object
		{
			
				var vo:Object=new v();
				var attNamesList:XMLList =x.@*;
				
				try{
				for (var j:int=0 ;j<x.elements().length();j++){
					var zx:XML=x.elements()[j] as XML;
					var pname:String=zx.localName();
					if(vo.hasOwnProperty(pname))
					{
						if(zx.elements().length()>0){
							vo[pname]=zx.elements();
						}else{
							if(typeof(vo[pname])=="boolean"){
								
								if(int(zx)==0){
									vo[pname]=false;
								}else if(int(zx)==1){
									vo[pname]=true;
								}
							}else{
								vo[pname]=zx;
							}
							
							
						}
						
					}
				}
				
				for (var i:int = 0; i < attNamesList.length(); i++)
				{
					var vname:String=attNamesList[i].name();
					if(vo.hasOwnProperty(vname))
					{
						var value:String=attNamesList[i];
						if(typeof(vo[vname])=="boolean"){
							if(int(value)==0){
								vo[vname]=false;
							}else if(int(value)==1){
								vo[vname]=true;
							}
						}else{
							vo[vname]=value;
						}
					}
				}
				
				}catch(e:Error){
					trace(e);
				}
				return vo;
			
		}
		public static function xmllisttovo(obj:Object,cls:Class=null):Array{
			var	arr:Array=[];
			var xml:XML;
			try{
				xml=XML(obj);
				var classname:String=xml.@classname;
//				var isList:int=xml.@isList;
				var xmllist:XMLList=xml.children();
				
				var myclass:Class;
				if(classname && classname.length){
					myclass=Class(getDefinitionByName(classname));
				}else if(cls){
					myclass=cls;
				} 
				
				for each(var node:XML in xmllist){
					arr.push(xmltovo(myclass,node));
					
				}
				
			}catch(e:Error){
				
			}
			
			
			return arr;
		}
		
		
			
		public static function votoxml(myObj:Object,comm:String='',isList:Boolean=false,templete:Boolean=true):String
		{
				var oldignore:Boolean=XML.ignoreComments;
				RFTraceSummary('comm',comm);
				XML.ignoreComments=false;
				XML.prettyPrinting=true;
				
//				var comment:String="!--"+comm+"--";
				var xml:XML=describeType(myObj);
				var classname:String=xml.@name.toString();
				var output:XML=<templete isList={int(isList)} classname={classname}>
					<!-- Generated by VOUtils.votoxml -->
				</templete>;
				
				var prop:XMLList=xml.child("factory");
				
				var vl:int=1;
				if(templete){
					vl=2;
				}
				
				for(var i:int=0;i<vl;i++){
					var ot:XML;
					if(isList){
						ot=<vo></vo>;
					}else{
						ot=<vo></vo>;
					}
            		for each(var item:XML in prop.child("variable")) {
            			var tp:String=item.@name;
            			var node:XML=<{tp}>{tp+i}</{tp}>;
            			ot.appendChild(node);
            		}
            		output.appendChild(ot);
          		}
          		
          		XML.ignoreComments=oldignore;
          	return output;
			
		}
		
	}
}